package com.kehutong.tag.admin;

import com.kehutong.common.DoveClient;
import com.kehutong.common.entity.Root;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import com.kehutong.crm.entity.Tag;
import com.kehutong.crm.enums.TagSourceType;
import com.kehutong.tag.entity.UserTag;
import com.kehutong.tag.service.ThirdPartyTagService;
import com.kehutong.crm.service.TagService;
import com.kehutong.tag.service.UserTagService;
import org.coraframework.authz.HasPermission;
import org.coraframework.authz.Subject;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.util.Objects;
import org.coraframework.util.UUIDGenerator;

import java.io.IOException;
import java.util.List;

/**
 * 第三方标签
 *
 * @author: liukunlong
 * @date: 2021-07-28 11:52
 */
@MvcAction
@ReqMapping("/admin/crm/third/party/tag")
public class ThirdPartyTagController {
    static final Logger logger = LoggerFactory.getLogger(ThirdPartyTagController.class);


    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private ThirdPartyTagService thirdPartyTagService;

    @Inject
    private DoveClient doveClient;

    /**
     * 根据条件获取标签列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("crm:third:party:tag:view")
    @ReqMapping("/page")
    public Page<Tag> page(JSONObject jsonObject) throws Exception {
        return thirdPartyTagService.page(jsonObject);
    }

    @HasPermission("crm:third:party:tag:view")
    @ReqMapping("/get")
    public Object get(Tag corpTagGroup) {
        return thirdPartyTagService.get(corpTagGroup);
    }


    @HasPermission("crm:third:party:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(Tag corpTag) {
        return thirdPartyTagService.save(corpTag);
    }


    @HasPermission("crm:third:party:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(Tag corpTag) throws Exception {
        return thirdPartyTagService.update(corpTag);
    }


    @HasPermission("crm:third:party:tag:edit")
    @ReqMapping("/delete")
    public Object delete(Tag corpTag) {
        return thirdPartyTagService.delete(corpTag);
    }

    /**
     * 根据条件获取标签组列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("crm:third:party:tag:view")
    @ReqMapping("/group/page")
    public Page<Tag> groupPage(JSONObject jsonObject) throws Exception {
        return thirdPartyTagService.groupPage(jsonObject);
    }


    @HasPermission("crm:third:party:tag:view")
    @ReqMapping("/group/list")
    public Object groupList(JSONObject jsonObject) throws Exception {
        return thirdPartyTagService.groupList(jsonObject);
    }

    @HasPermission("crm:third:party:tag:view")
    @ReqMapping("/group/get")
    public Object groupGet(Tag corpTagGroup) {
        return thirdPartyTagService.groupGet(corpTagGroup);
    }


    @HasPermission("crm:third:party:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/group/save")
    public Object groupSave(Token token, JSONObject jsonObject) throws Exception{
        String name = jsonObject.getString("name");
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        String groupId=ThirdPartyTagService.toKhtId(token.getCompanyNo(),"GROUP",name);
        Tag corpTagGroup = Pool.newInstance(Tag.class,groupId);
        corpTagGroup.setCompanyNo(token.getCompanyNo());
        corpTagGroup.setPid(Root.ROOT_ID);
        corpTagGroup.setGroup(true);
        corpTagGroup.setName(name);
        corpTagGroup.setCreateBy(Root.ROOT_ID);
        corpTagGroup.setCreateName("系统");
        corpTagGroup.setSort(1);
        List<Tag> childList = JSONObject.parseArray(jsonArray.toJSONString(), Tag.class);
        corpTagGroup.setChildList(childList);
        thirdPartyTagService.groupSave(corpTagGroup);

        Tag syncTag= (Tag) thirdPartyTagService.groupGet(jdbcSession.findById(Tag.class,groupId));
        //内建的才能创建标签同步到企微
        doveClient.post("/wwin/tag/wwin/create",(http)-> {
            http.addHeader("companyNo", token.getCompanyNo());
            http.setBody(JSONObject.toJSONString(syncTag));
        });

        return Result.success();
    }

    @HasPermission("crm:third:party:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/group/update")
    public Object groupUpdate(Token token,JSONObject jsonObject) throws Exception {
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        if(Objects.nonEmpty(jsonArray)){
            for (Object tagJsonObj : jsonArray) {
                JSONObject tagJson = (JSONObject) tagJsonObj;
                tagJson.remove("type");
                tagJson.remove("thirdPartySource");
            }
        }
        jsonObject.remove("childList");
        jsonObject.remove("type");
        jsonObject.remove("thirdPartySource");
        Tag corpTagGroup = JSONObject.parseObject(jsonObject.toJSONString(), Tag.class);
        List<Tag> childList = JSONObject.parseArray(jsonArray.toJSONString(), Tag.class);
        corpTagGroup.setChildList(childList);
        corpTagGroup.setCompanyNo(token.getCompanyNo());
        return thirdPartyTagService.groupUpdate(corpTagGroup);
    }


    @HasPermission("crm:third:party:tag:edit")
    @ReqMapping("/group/delete")
    public Object groupDelete(Tag corpTagGroup) {
        return thirdPartyTagService.groupDelete(corpTagGroup);
    }
}
